// pages/user/history.js
import { Activity } from '../../model/activity';
import Dialog from '@vant/weapp/dialog/dialog';
const appInst =  getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    order_list: [],
    user_list: [],
    pay_loading: false,
    show: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      wx.showLoading({
        title: '加载中',
      });
      this.getOrderList();
  },
  onClickShow() {
    this.setData({ show: true });
  },
  onClickHide() {
    this.setData({ show: false });
  },
  /**
   * 获取订单信息
   */
  async getOrderList() {
    const res = await Activity.getActivityOrder({});
    console.log(res);
    this.setData({
      order_list: res
    })
    this.getGrActivityUser();
  },
  /**
   * 获取个人创建信息
   */
  async getGrActivityUser(){
    const res = await Activity.getGrActivityUser();
    this.setData({
      user_list: res
    });
    wx.hideLoading();
  },
  /**
   * 去支付
   */
  goPay(e) {
    const self = this;
    const out_trade_no = e.currentTarget.dataset.no;
    if(this.data.pay_loading){
      return false;
    }
    this.setData({
      pay_loading: true,
    })
    wx.showLoading({
      title: '支付中',
    });
    appInst.preOrder(out_trade_no,function (){
      self.changeLoading();
    });
  },
  /**
   * 关闭弹框以及加载
   */
  changeLoading() {
    this.setData({
      pay_loading: false,
    });
    wx.hideLoading();
  },
  /**
   * 删除活动订单
   */
  delOrder(e) {
    const self = this;
    const id = e.currentTarget.dataset.id;
    const order_type = e.currentTarget.dataset.type;
    Dialog.confirm({
      message: '是否删除订单?',
    })
    .then(async () => {
      // on confirm
      wx.showLoading({
        title: '删除中',
      });
      const res = await Activity.deleteActivityOrder({
        id: id,
        order_type: order_type,
      });
      if(res) {
        self.getOrderList();
      }
    })
  },
  /**
   * 打开官方活动详情
   */
  openGf(e) {
    wx.navigateTo({
      url: "../activity/official?id="+e.currentTarget.dataset.id
    })
  },
  /**
   * 打开个人活动详情
   */
  openGr(e) {
    wx.navigateTo({
      url: "../activity/create_personal?id="+e.currentTarget.dataset.id
    })
  },
  /**
   * 打开商家活动详情
   */
  openSj(e) {
    wx.navigateTo({
      url: "../activity/business_info?id="+e.currentTarget.dataset.id
    })
  }
})